﻿using System.Xml;
using System.Xml.Serialization;
using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Schema;

namespace ScormModel
{
    [Serializable()]
    public class RollupRule
    {
        private string _childActivitySet ;
        private string _minimumCount;
        private string _minimumPercent;
         [NonSerialized]
        private XmlNode _rollupRuleContent;
        private RollupConditions _rollupConditions;
        private RollupAction _rollupAction;
        [XmlAttribute("childActivitySet")]
        public string ChildActivitySet
        {
            get { return this._childActivitySet; }
            set { this._childActivitySet = value; }
        }
        [XmlAttribute("minimumCount")]
        public string MinimumCount
        {
            get { return this._minimumCount; }
            set { this._minimumCount = value; }
        }

        [XmlAttribute("minimumPercent")]
        public string MinimumPercent
        {
            get { return this._minimumPercent; }
            set { this._minimumPercent = value; }
        }

        [XmlElement("rollupConditions", Namespace = "http://www.imsglobal.org/xsd/imsss")]
        public RollupConditions RollupConditions
        {
            get { return this._rollupConditions; }
            set { this._rollupConditions = value; }
        }

        [XmlElement("rollupAction", Namespace = "http://www.imsglobal.org/xsd/imsss")]
        public RollupAction RollupAction
        {
            get { return this._rollupAction; }
            set { this._rollupAction = value; }
        }
        
        [XmlIgnoreAttribute]
        public XmlNode RollupRuleContent
        {
            get { return this._rollupRuleContent; }
            set { this._rollupRuleContent = value; }
        }
        public void buildRollupRule()
        {
            XmlAttributeCollection attList = _rollupRuleContent.Attributes;
            if (attList.GetNamedItem("childActivitySet") != null)
            {
                _childActivitySet = ((XmlAttribute)attList.GetNamedItem("childActivitySet")).Value;
            }
            if (attList.GetNamedItem("minimumCount") != null)
            {
                _minimumCount = ((XmlAttribute)attList.GetNamedItem("minimumCount")).Value;
            }
            if (attList.GetNamedItem("minimumPercent") != null)
            {
                _minimumPercent = ((XmlAttribute)attList.GetNamedItem("minimumPercent")).Value;
            }
            for (int i = 0; i < _rollupRuleContent.ChildNodes.Count; i++)
            {
                if (((XmlNode)(_rollupRuleContent.ChildNodes[i])).Name == "imsss:rollupConditions")
                {
                    _rollupConditions = new RollupConditions();
                    _rollupConditions.RollupConditionsContent = _rollupRuleContent.ChildNodes[i];
                    _rollupConditions.buildRollupConditions();
                }
                if (((XmlNode)(_rollupRuleContent.ChildNodes[i])).Name == "imsss:rollupAction")
                {
                    _rollupAction = new RollupAction();
                    _rollupAction.RollupActionContent = _rollupRuleContent.ChildNodes[i];
                    _rollupAction.buildRollupAction();
                }
            }
        }
    }
}
